package com.cos.openapi.web.controller;

import com.cos.openapi.common.Const;
import com.cos.openapi.common.ResponseResult;
import com.cos.openapi.common.ResultTable;
import com.cos.openapi.entity.AdminUser;
import com.cos.openapi.services.AdminUserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author: COS
 * @time: 2021/11/23 16:41
 * @description:
 */
@RestController
@RequestMapping("/user")
public class AdminUserController {
    @Autowired
    private AdminUserServices userServices;

    @RequestMapping("/login")
    public ResponseResult login(String email, String password, HttpSession session) {
        AdminUser user = userServices.doLogin(email, password);
        if (user != null) {
            session.setAttribute(Const.IS_LOGIN, user);
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("用户名或密码不正确,请检查脑子");
    }


    @RequestMapping("/logout")
    public void logout(HttpSession session, HttpServletResponse response) throws IOException {
        session.invalidate();
        //设置手动进行重定向
        response.sendRedirect("/login.html");
    }

    @RequestMapping("/table")
    public ResultTable table(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer limit,
                             String realName,
                             String email,
                             Integer status) {

        return userServices.table(page, limit, realName, email, status);
    }


    @RequestMapping("/user_role")
    public List<Integer> getUserRole(Integer userId) {
        return userServices.getUserRole(userId);
    }

    @RequestMapping("/assign_role")
    public ResponseResult assignRole(Integer userId, Integer[] roleIds) {
        try {
            return userServices.assignRole(userId, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("操作失败");
    }

    @RequestMapping("/del")
    public ResponseResult del(Integer[] ids) {

        try {
            return userServices.delUsers(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("删除失败");
    }


    @RequestMapping("/update")
    public ResponseResult updateUser(AdminUser user) {
        try {
            return userServices.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("修改失败");
    }

    @RequestMapping("/add")
    public ResponseResult addUser(AdminUser user) {
        try {
            return userServices.addUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("新增失败");
    }


}
